<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?><?
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Mimemail_model extends CI_Model {

    function send_mime_mail($name_from,
                            $email_from,
                            $name_to,
                            $email_to,
                            $data_charset,
                            $send_charset,
                            $subject,
                            $body
                            ) {
      $to = $this->mime_header_encode($name_to, $data_charset, $send_charset)
                     . ' <' . $email_to . '>';
      $subject = $this->mime_header_encode($subject, $data_charset, $send_charset);
      $from =  $this->mime_header_encode($name_from, $data_charset, $send_charset)
                         .' <' . $email_from . '>';
      if($data_charset != $send_charset) {
        $body = iconv($data_charset, $send_charset, $body);
      }
      $headers = "From: $from\r\n";
      $headers .= "Content-type: text/plain; charset=$send_charset\r\n";

      return mail($to, $subject, $body, $headers);
    }

    function mime_header_encode($str, $data_charset, $send_charset) {
      if($data_charset != $send_charset) {
        $str = iconv($data_charset, $send_charset, $str);
      }
      return '=?' . $send_charset . '?B?' . base64_encode($str) . '?=';
    }

	function xmail( $from, $to, $subj, $text, $filename) {
	$f         = fopen($filename,"rb");
	$un        = strtoupper(uniqid(time()));
	$head      = "From: $from\n";
	$head     .= "To: $to\n";
	$head     .= "Subject: $subj\n";
	$head     .= "X-Mailer: PHPMail Tool\n";
	$head     .= "Reply-To: $from\n";
	$head     .= "Mime-Version: 1.0\n";
	$head     .= "Content-Type:multipart/mixed;";
	$head     .= "boundary=\"----------".$un."\"\n\n";
	$zag       = "------------".$un."\nContent-Type:text/plain;\n";
	$zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";
	$zag      .= "------------".$un."\n";
	$zag      .= "Content-Type: application/octet-stream;";
	$zag      .= "name=\"".basename($filename)."\"\n";
	$zag      .= "Content-Transfer-Encoding:base64\n";
	$zag      .= "Content-Disposition:attachment;";
	$zag      .= "filename=\"".basename($filename)."\"\n\n";
	$zag      .= chunk_split(base64_encode(fread($f,filesize($filename))))."\n";

	if (!@mail("$to", "$subj", $zag, $head))
	 return false;
	else
	 return true;
	}

}
?>